package cn.jja8.bukkit.bukkitcommandtool.command;

import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.plugin.Plugin;

import java.lang.reflect.Constructor;
import java.util.List;

public class CommandMangering  implements CommandExecutor, TabCompleter {
    /**插件命令管理类*/
    private final PluginCommand pluginCommand;
    /**主命令*/
    private final String mainCommand;
    /**别称*/
    private final List<String> aliases;
    /**命令执行器*/
    private final Command command;
    /**
     * 加载并获得命令控制器
     * @param main 插件的主类
     * @param command  */
    public CommandMangering(Plugin main, Command command){
        this.mainCommand = command.getMainCommand();
        this.aliases = command.getAliases();
        this.command = command;


        Class CraftServerClass = null; //org.bukkit.craftbukkit.v1_16_R2.CraftServer.class;
        Class<PluginCommand> clas = PluginCommand.class;
        PluginCommand pluginCommand = null;
        SimpleCommandMap CommandMap = null;
        try {
            String className = Bukkit.getServer().getClass().getName();
            CraftServerClass = Class.forName(className);
            //noinspection unchecked
            Object simpleCommandMap = CraftServerClass.getDeclaredMethod("getCommandMap").invoke(Bukkit.getServer());
            CommandMap = simpleCommandMap instanceof SimpleCommandMap ? (SimpleCommandMap)simpleCommandMap : null;
            Constructor<PluginCommand> constructor = clas.getDeclaredConstructor(String.class, Plugin.class);
            constructor.setAccessible(true);
            pluginCommand = constructor.newInstance(mainCommand,main);
           if(aliases!=null)pluginCommand.setAliases(aliases);
            pluginCommand.setExecutor(this);
            pluginCommand.setTabCompleter(this);

        }catch (Exception e) {
            e.printStackTrace();
            main.getLogger().severe("---------------------------------------");
            main.getLogger().severe("bukkit-command-tool依赖库在不受支持的版本的服务器上运行！");
            main.getLogger().severe("mns:"+Bukkit.getServer().getClass().getName());
            main.getLogger().severe("若有疑问，您可以前往 “PlugClub/插件实验室 - 820131534” 交流。");
            main.getLogger().severe("---------------------------------------");
        }
        if(pluginCommand!=null&CommandMap!=null){
            CommandMap.register(main.getName(),pluginCommand);
            this.pluginCommand = pluginCommand;
        }else {
            throw new RuntimeException("patronSaint依赖库无法加插件“"+main.getName()+"”的“"+mainCommand+"”命令,你的服务器是我们可以兼容的版本吗？");
        }
    }

    /**
     * 获取pluginCommand
     * */
    public PluginCommand getPluginCommand() {
        return pluginCommand;
    }


    //非aip-------------------

    @Override
    public boolean onCommand(CommandSender commandSender,org.bukkit.command.Command command, String s, String[] strings) {
        return this.command.command(commandSender,strings);
    }

    @Override
    public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String s, String[] strings) {
        return this.command.tabCompletion(commandSender,strings);
    }
}
